<template>
  <view class="group-manage">
    <!-- 筛选tab -->
    <view class="tabs">
      <view
        v-for="s in statusList"
        :key="s.value"
        :class="['tab', { active: s.value === jobStatus }]"
        @click="changeStatus(s.value)"
      >
        {{ s.label }}
      </view>
    </view>

    <!-- 邀请列表 -->
    <view v-if="list.length" class="invite-list">
      <view v-for="item in list" :key="item.id" class="invite-item">
        <view class="invite-main">
          <text class="nickname">{{ item.nickname }}</text>
          <text class="date">{{ item.invitation_date }}</text>
          <text :class="['status', statusClass(item.job_status)]">
            {{ statusText(item.job_status) }}
          </text>
        </view>
        <view v-if="item.job_status === 5 && item.invitation_user" class="invited-user">
          <image
            class="avatar"
            :src="item.invitation_user.avatar || '/static/avatar-default.png'"
            mode="aspectFill"
          />
          <text class="invited-nickname">{{ item.invitation_user.nickname }}</text>
        </view>
      </view>
    </view>
    <view v-else class="empty">
      <image src="/static/empty.png" mode="aspectFit" class="empty-icon"></image>
      <text class="empty-text">暂无邀请记录</text>
    </view>

    <!-- 分页加载 -->
    <button
      v-if="hasMore && list.length"
      class="load-more"
      @click="loadMore"
      :disabled="loading"
    >
      {{ loading ? '加载中...' : '加载更多' }}
    </button>

    <!-- 悬浮新增邀请按钮 -->
    <button class="fab-add-invite" @click="goToInvite">＋</button>
  </view>
</template>

<script>
import request from '@/utils/request.js'

export default {
  data() {
    return {
      statusList: [
        { label: '全部', value: '' },
        { label: '邀请中', value: 1 },
        { label: '邀请成功', value: 5 },
        { label: '邀请失败', value: 7 }
      ],
      jobStatus: '',
      list: [],
      page: 1,
      hasMore: true,
      loading: false,
    }
  },
  methods: {
    async fetchList(reset = false) {
      if (reset) {
        this.page = 1
        this.list = []
        this.hasMore = true
      }
      if (!this.hasMore || this.loading) return
      this.loading = true
      try {
        const res = await request({
          url: '/wechat/user/invitationLists',
          method: 'POST',
          data: {
            job_status: this.jobStatus,
            page: this.page
          }
        })
        if (res.code === 0 && res.data) {
          const data = res.data.data || []
          this.list = reset ? data : this.list.concat(data)
          this.hasMore = !!res.data.next_page_url
          this.page++
        } else {
          uni.showToast({ title: res.message || '加载失败', icon: 'none' })
        }
      } catch (e) {
        uni.showToast({ title: '网络错误', icon: 'none' })
      } finally {
        this.loading = false
      }
    },
    changeStatus(status) {
      if (this.jobStatus !== status) {
        this.jobStatus = status
        this.fetchList(true)
      }
    },
    statusText(status) {
      switch (status) {
        case 1: return '邀请中'
        case 5: return '邀请成功'
        case 7: return '邀请失败'
        default: return '未知状态'
      }
    },
    statusClass(status) {
      switch (status) {
        case 1: return 'status-pending'
        case 5: return 'status-success'
        case 7: return 'status-fail'
        default: return ''
      }
    },
    loadMore() {
      this.fetchList()
    },
    goToInvite() {
      uni.navigateTo({ url: '/pages/group/group-invite' })
    },
  },
  onShow() {
    this.fetchList(true)
  },
}
</script>

<style scoped>
.group-manage {
  padding: 24rpx 20rpx 40rpx 20rpx;
}
.tabs {
  display: flex;
  margin-bottom: 24rpx;
}
.tab {
  flex: 1;
  text-align: center;
  padding: 16rpx 0;
  font-size: 30rpx;
  color: #888;
  border-bottom: 4rpx solid transparent;
  transition: color 0.2s, border-color 0.2s;
}
.tab.active {
  color: var(--primary, #2979ff);
  border-bottom: 4rpx solid var(--primary, #2979ff);
  font-weight: bold;
}
.invite-list {
  margin-bottom: 32rpx;
}
.invite-item {
  background: var(--card-bg, #fff);
  border-radius: 12rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
  padding: 24rpx 20rpx;
  margin-bottom: 20rpx;
}
.invite-main {
  display: flex;
  align-items: center;
  gap: 20rpx;
}
.nickname {
  font-size: 30rpx;
  font-weight: 500;
  color: var(--text, #222);
}
.date {
  font-size: 26rpx;
  color: #888;
}
.status {
  margin-left: auto;
  font-size: 26rpx;
  padding: 2rpx 16rpx;
  border-radius: 16rpx;
}
.status-pending {
  background: #f5f7fa;
  color: #2979ff;
}
.status-success {
  background: #e8f5e9;
  color: #43a047;
}
.status-fail {
  background: #fbe9e7;
  color: #e53935;
}
.invited-user {
  display: flex;
  align-items: center;
  margin-top: 12rpx;
  gap: 12rpx;
}
.avatar {
  width: 56rpx;
  height: 56rpx;
  border-radius: 50%;
  background: #eee;
}
.invited-nickname {
  font-size: 28rpx;
  color: var(--text, #222);
}
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 120rpx;
  margin-bottom: 60rpx;
}
.empty-icon {
    width: 240rpx;
    height: 240rpx;
    margin-bottom: 24rpx;
}
.empty-text {
  font-size: 32rpx;
  color: #bbb;
}
.load-more {
  width: 100%;
  height: 64rpx;
  background: var(--primary, #2979ff);
  color: #fff;
  border: none;
  border-radius: 8rpx;
  font-size: 28rpx;
  margin-top: 12rpx;
}
.load-more[disabled] {
  background: #ccc;
  color: #fff;
}
.fab-add-invite {
  position: fixed;
  right: 48rpx;
  bottom: 80rpx;
  width: 104rpx;
  height: 104rpx;
  border-radius: 50%;
  background: var(--primary, #2979ff);
  color: #fff;
  font-size: 60rpx;
  box-shadow: 0 8rpx 32rpx rgba(41,121,255,0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1001;
  border: none;
  outline: none;
  transition: background 0.2s;
}
.fab-add-invite:active {
  background: #1760d6;
}
</style> 